package com.twu.biblioteca;

/**
 * Created by aswathik on 6/11/14.
 */
public class ReturnBook implements MenuItem {

    private Library library;
    private String menuString;

    ReturnBook(Library library) {
        this.library = library;
        menuString = "Return Book";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ReturnBook that = (ReturnBook) o;

        if (library != null ? !library.equals(that.library) : that.library != null) return false;
        if (menuString != null ? !menuString.equals(that.menuString) : that.menuString != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = library != null ? library.hashCode() : 0;
        result = 31 * result + (menuString != null ? menuString.hashCode() : 0);
        return result;
    }

    public String getMenuString() {
        return menuString;
    }

    public MenuActionResult performMenuAction(UserInterface userInterface) {

        LoginValidator loginValidator = new LoginValidator(userInterface);
        MenuActionResult menuActionResult = new MenuActionResult();

//        if (!loginValidator.validateLogin()) {
//            menuActionResult.setMenuActionDetails(false,"Invalid login details!");
//            return menuActionResult;
//        }
        String bookTitle = userInterface.getBookTitle();
        String bookAuthor = userInterface.getBookAuthor();
        int yearOfPublication = userInterface.getBookYearOfPublication();
        LibraryItem bookToCheckOut = new Book(bookTitle,bookAuthor,yearOfPublication);

        LibraryItem bookToReturn = new Book(bookTitle,bookAuthor,yearOfPublication);

        if (library.returnItem(bookToReturn)) {
            menuActionResult.setMenuActionDetails(true, "Thank you for returning the book!");
        } else {
            menuActionResult.setMenuActionDetails(false, "Sorry that book is not a valid book to return!");
        }
        userInterface.displayMenuActionResult(menuActionResult);
        return menuActionResult;
    }
}
